﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
/// <summary>
/// Summary description for WebTool
/// </summary>
public class WebTool
{
    public WebTool()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region "Method"
    public static string getWebContent(string pUrl)
    {
        string retContent = "";
        if (pUrl != "")
        {
            try
            {
                //System.Net.WebClient myClient = new System.Net.WebClient();
                //myClient.Encoding = System.Text.Encoding.UTF8;
                //System.IO.Stream myStream = myClient.OpenRead(pUrl);
                //System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myStream, System.Text.Encoding.UTF8, true);

                
                UTF8Encoding enc = new UTF8Encoding();
                WebClient browser = new WebClient();
                retContent = enc.GetString(browser.DownloadData(pUrl));

                //F1
                //string NewLine;
                //loop through the stream and put the output in the textbox.
                //while ((NewLine = myStreamReader.ReadLine()) != null)
                //    txOutput += NewLine + "\r\n";

                //txOutput = myStreamReader.ReadToEnd();
                //myStream.Close();
            }
            catch (Exception ex)
            {
                TLog.WriteErr("WebTool:getWebContent", ex.Message);
            }
        }
        return retContent;
    }

    public static string getWebClient(string pUrl)
    {
        string retContent = "";
        HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(pUrl);
        WebRequestObject.UserAgent = ".NET Framework/2.0";
        //WebRequestObject.Referer = "http://www.techcombank.com.vn/Desktop.aspx/Thong-tin-ty-gia/Ty-gia";
        WebResponse Response = WebRequestObject.GetResponse();

        // Open data stream:
        Stream WebStream = Response.GetResponseStream();

        // Create reader object:
        StreamReader Reader = new StreamReader(WebStream);

        // Read the entire stream content:
        retContent = Reader.ReadToEnd();

        // Cleanup
        Reader.Close();
        WebStream.Close();
        Response.Close();
        return retContent;
    }

    public static string PostData(string URL, string strPostData)
    {
        string html = "";
        HttpWebRequest WebRequestObject = WebRequest.Create(URL) as HttpWebRequest;
        WebRequestObject.Timeout = 50000;
        WebRequestObject.Method = "POST";
        WebRequestObject.ContentLength = strPostData.Length;
        WebRequestObject.ContentType = "application/x-www-form-urlencoded";
        WebRequestObject.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17";
        WebRequestObject.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        WebRequestObject.KeepAlive = true;
        WebRequestObject.Accept = "*/*";
       /*  WebRequestObject.Headers.Add("Host", "www.easy-forex.com");
        WebRequestObject.Headers.Add("Cookie",  ".ASPXANONYMOUS=YNQrCzwozgEkAAAAMmRiYmQ2ZWItMjY4Zi00MGFkLTg2NDctOTA3ZDk3NGEyZTgz0; BaseSite=EVO; cor=7fbdf6486f47000032dcf35084e50600e9050100; PumaCurrentCulture=Int-en; session_start_time=1358944881295; k_visit=4; auth=%7B%22sessionId%22%3A%223666_c742db53-c2e2-4272-be7a-1d4b47833432%22%2C%22isAuthenticated%22%3Afalse%7D; EZFX.Start=1358945171516; LSCulture=int-en; Dragon=1; __utma=1.1507004769.1358094199.1358927127.1358944882.5; __utmb=1.3.10.1358944882; __utmc=1; __utmz=1.1358927127.4.4.utmcsr=vgb.com.vn|utmccn=(referral)|utmcmd=referral|utmcct=/Pages/Default.aspx");
        WebRequestObject.Referer = "http://www.easy-forex.com/int/currencyratespage";
        WebRequestObject.
 Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
 Accept-Encoding:gzip,deflate,sdch
 Accept-Language:en-US,en;q=0.8
 Connection:keep-alive
 Content-Length:214
 Content-Type:application/x-www-form-urlencoded
 Cookie:.ASPXANONYMOUS=YNQrCzwozgEkAAAAMmRiYmQ2ZWItMjY4Zi00MGFkLTg2NDctOTA3ZDk3NGEyZTgz0; BaseSite=EVO; cor=7fbdf6486f47000032dcf35084e50600e9050100; PumaCurrentCulture=Int-en; session_start_time=1358944881295; k_visit=4; auth=%7B%22sessionId%22%3A%223666_c742db53-c2e2-4272-be7a-1d4b47833432%22%2C%22isAuthenticated%22%3Afalse%7D; EZFX.Start=1358945171516; LSCulture=int-en; Dragon=1; __utma=1.1507004769.1358094199.1358927127.1358944882.5; __utmb=1.3.10.1358944882; __utmc=1; __utmz=1.1358927127.4.4.utmcsr=vgb.com.vn|utmccn=(referral)|utmcmd=referral|utmcct=/Pages/Default.aspx
 Host:www.easy-forex.com
 Origin:http://www.easy-forex.com
 Referer:http://www.easy-forex.com/int/currencyratespage/
 User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17
 X-Requested-With:XMLHttpRequest
         */


        try
        {
            // Post to the login form.
            StreamWriter swRequestWriter = new
            StreamWriter(WebRequestObject.GetRequestStream());
            swRequestWriter.Write(strPostData);
            swRequestWriter.Close();
            using (System.Net.HttpWebResponse hwrWebResponse = (System.Net.HttpWebResponse)WebRequestObject.GetResponse())
            {
                bool isSuccess = (int)hwrWebResponse.StatusCode < 299 && (int)hwrWebResponse.StatusCode >= 200;
                if (isSuccess)
                {
                    CookieCollection ccCookies = hwrWebResponse.Cookies;
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(hwrWebResponse.GetResponseStream()))
                    {
                        html = reader.ReadToEnd();
                        reader.Close();
                    }
                }
                else { html = "Error"; }
            }
        }
        catch (System.Net.WebException ex)
        {
            html = "";
            TLog.WriteErr(string.Format("WebTool:getWebContent input:{0}", URL), ex.Message);
        }
        return html;

    }
    public static string PostData2(string URL, string strPostData)
    {
        string html = "";
        HttpWebRequest WebRequestObject = WebRequest.Create(URL) as HttpWebRequest;
        WebRequestObject.Timeout = 50000;
        WebRequestObject.Method = "POST";
        WebRequestObject.ContentLength = strPostData.Length;
        WebRequestObject.ContentType = "application/x-www-form-urlencoded";
        WebRequestObject.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17";
        WebRequestObject.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        WebRequestObject.KeepAlive = true;
        WebRequestObject.Accept = "*/*";
        try
        {
            // Post to the login form.
            StreamWriter swRequestWriter = new
            StreamWriter(WebRequestObject.GetRequestStream());
            swRequestWriter.Write(strPostData);
            swRequestWriter.Close();
            using (System.Net.HttpWebResponse hwrWebResponse = (System.Net.HttpWebResponse)WebRequestObject.GetResponse())
            {
                bool isSuccess = (int)hwrWebResponse.StatusCode < 299 && (int)hwrWebResponse.StatusCode >= 200;
                if (isSuccess)
                {
                    CookieCollection ccCookies = hwrWebResponse.Cookies;
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(hwrWebResponse.GetResponseStream()))
                    {
                        html = reader.ReadToEnd();
                        reader.Close();
                    }
                }
                else { html = "Error"; }
            }
        }
        catch (System.Net.WebException ex)
        {
            html = "";
            throw (ex);
        }
        return html;

    }
    #endregion
}
